<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="category" label="" required>
        <uni-easyinput placeholder="栏目类别，如个人证件、企业证件等" v-model="formData.category" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="subCategory" label="" required>
        <uni-easyinput placeholder="子类别，如身份证、营业执照等" v-model="formData.subCategory" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="name" label="" required>
        <uni-easyinput placeholder="联系人姓名" v-model="formData.name" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="phone" label="" required>
        <uni-easyinput placeholder="联系电话" v-model="formData.phone" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="content" label="" required>
        <uni-easyinput placeholder="见报内容" v-model="formData.content" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="publishDate" label="" required>
        <uni-easyinput placeholder="刊登日期，格式如：20200301,20200302" v-model="formData.publishDate" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="publishDays" label="" required>
        <uni-easyinput placeholder="刊登天数" type="number" v-model="formData.publishDays"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="needInvoice" label="">
        <switch @change="binddata('needInvoice', $event.detail.value)" :checked="formData.needInvoice"></switch>
      </uni-forms-item>
      <uni-forms-item name="price" label="" required>
        <undefined v-model="formData.price"></undefined>
      </uni-forms-item>
      <uni-forms-item name="paymentMethod" label="">
        <uni-data-checkbox v-model="formData.paymentMethod" :localdata="formOptions.paymentMethod_localdata" trim="both"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="status" label="" required>
        <uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata" trim="both"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="createTime" label="" required>
        <uni-datetime-picker return-type="date" v-model="formData.createTime"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="updateTime" label="">
        <uni-datetime-picker return-type="date" v-model="formData.updateTime"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="remark" label="">
        <uni-easyinput placeholder="备注" v-model="formData.remark" trim="both"></uni-easyinput>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/ad_info.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'ad_info';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "category": "",
        "subCategory": "",
        "name": "",
        "phone": "",
        "content": "",
        "publishDate": "",
        "publishDays": 1,
        "needInvoice": null,
        "price": null,
        "paymentMethod": "",
        "status": "",
        "createTime": null,
        "updateTime": null,
        "remark": ""
      }
      return {
        formData,
        formOptions: {
          "paymentMethod_localdata": [
            {
              "value": "wechat",
              "text": "wechat"
            },
            {
              "value": "alipay",
              "text": "alipay"
            }
          ],
          "status_localdata": [
            {
              "value": "pending",
              "text": "pending"
            },
            {
              "value": "processing",
              "text": "processing"
            },
            {
              "value": "completed",
              "text": "completed"
            },
            {
              "value": "cancelled",
              "text": "cancelled"
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
